// pages/login/login.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		top: '',
		height: '',

		username: 'admin',
		password: 'wzn***123',
		// username: 'guoli',
		// password: 'guoli***123'
		username: '',
		password: '',
		userInfo: {},

		wxCode: '',
	},
	login() {
		wx.showLoading({
			title: '加载中...'
		});
		http.post({
			url: '/system/auth/login',
			header: {
				"content-type": "application/json"
			},
			data: {
				username: this.data.username,
				password: this.data.password
			},
			then: (res) => {
				console.log('登录', res);
				if (res.data.code == 0) {
					wx.setStorageSync('token', res.data.data.accessToken)
					wx.switchTab({
						url: '../home/home',
					})
				} else if (res.data.code == 400 || res.data.code == 1002000000) {
					wx.showToast({
						title: '账号或密码错误',
						icon: 'error',
					})
				} else {
					wx.showToast({
						title: res.msg,
						icon: 'error',
					})
				}
			}
		})
	},
	wxLogin() {
		wx.login({
			success: res => {
				if (res.code) {
					console.log('微信登录');
					this.setData({
						wxCode: res.code
					})
				} else {
					console.log('登录失败！' + res.errMsg);
				}
			}
		});
	},
	elseLogin(e) {
		let detail = e.detail
		if (detail.errMsg = 'getPhoneNumber:ok') {
			console.log('用户同意授权', detail);
			wx.showLoading({
				title: '加载中...'
			});
			http.post({
				url: '/system/user/wxLogin/getPhone',
				header: {
					"content-type": "application/json"
				},
				data: {
					code: detail.code
				},
				then: (res) => {
					console.log('微信授权登录', res);
					http.post({
						url: '/system/auth/login',
						header: {
							"content-type": "application/json"
						},
						data: {
							username: res.data.data.phoneNumber,
							password: 'wzn***123'
						},
						then: (res) => {
							console.log('登录', res);
							if (res.data.code == 0) {
								wx.setStorageSync('token', res.data.data.accessToken)
								http.get({
									url: '/system/user/get',
									data: {
										id: res.data.data.userId,
									},
									then: (res) => {
										console.log('个人资料', res);
										let userInfo = res.data.data
										if (!userInfo.projectIds) {
											userInfo.projectIds = [
												[66, 67],
												[66, 64],
												[2, 58],
												[2, 57],
												[2, 13],
												[2, 6]
											]
											http.put({
												url: '/system/user/update',
												data: userInfo,
												then: (res) => {
													console.log('设置项目权限', res);
													wx.switchTab({
														url: '../home/home',
													})
												}
											})
										} else {
											wx.switchTab({
												url: '../home/home',
											})
										}
									}
								})
							} else if (res.data.code == 400 || res.data.code == 1002000000) {
								wx.showToast({
									title: '账号或密码错误',
									icon: 'error',
								})
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'error',
								})
							}
						}
					})
				}
			})
		} else {
			wx.hideLoading()
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.setData({
			top: wx.getStorageSync('customTop'),
			height: wx.getStorageSync('customHeight'),
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})